﻿using System;
using System.Windows;
using System.Windows.Media.Animation;

namespace SilverFlow.Controls
{
    /// <summary>
    /// Useful Silverlight animation extensions
    /// </summary>
    public static class AnimationExtensions
    {
        /// <summary>
        /// Moves the object on Canvas in vertical direction.
        /// </summary>
        /// <param name="target">The target object to move.</param>
        /// <param name="to">Animation's ending value.</param>
        /// <param name="milliseconds">Duration of the animation in milliseconds.</param>
        /// <param name="easingFunction">Easing function applied to the animation.</param>
        /// <param name="completed">Event handler called when animation completed.</param>
        public static void MoveCanvasTop(this DependencyObject target, double? to, double milliseconds,
            IEasingFunction easingFunction = null, EventHandler completed = null)
        {
            Duration duration = new Duration(TimeSpan.FromMilliseconds(milliseconds));
            DoubleAnimation doubleAnimation = new DoubleAnimation()
            {
                To = to,
                Duration = duration,
                EasingFunction = easingFunction
            };

            Storyboard storyboard = new Storyboard();
            storyboard.Duration = duration;
            storyboard.Children.Add(doubleAnimation);

            Storyboard.SetTarget(doubleAnimation, target);
            Storyboard.SetTargetProperty(doubleAnimation, new PropertyPath("(Canvas.Top)"));

            if (completed != null)
                storyboard.Completed += completed;

            storyboard.Begin();
        }

        /// <summary>
        /// Moves and resizes the object.
        /// </summary>
        /// <param name="target">The target object.</param>
        /// <param name="position">Ending position.</param>
        /// <param name="width">New width.</param>
        /// <param name="height">New height.</param>
        /// <param name="milliseconds">Duration of the animation in milliseconds.</param>
        /// <param name="completed">Event handler called when animation completed.</param>
        public static void MoveAndResize(this DependencyObject target, Point position, double width, double height,
            double milliseconds, EventHandler completed = null)
        {
            Duration duration = new Duration(TimeSpan.FromMilliseconds(milliseconds));
            DoubleAnimation doubleAnimation1 = new DoubleAnimation();
            DoubleAnimation doubleAnimation2 = new DoubleAnimation();
            PointAnimation pointAnimation = new PointAnimation();

            doubleAnimation1.Duration = duration;
            doubleAnimation2.Duration = duration;
            pointAnimation.Duration = duration;

            Storyboard storyboard = new Storyboard();
            storyboard.Duration = duration;

            storyboard.Children.Add(doubleAnimation1);
            storyboard.Children.Add(doubleAnimation2);
            storyboard.Children.Add(pointAnimation);

            Storyboard.SetTarget(doubleAnimation1, target);
            Storyboard.SetTarget(doubleAnimation2, target);
            Storyboard.SetTarget(pointAnimation, target);

            Storyboard.SetTargetProperty(doubleAnimation1, new PropertyPath("(Width)"));
            Storyboard.SetTargetProperty(doubleAnimation2, new PropertyPath("(Height)"));
            Storyboard.SetTargetProperty(pointAnimation, new PropertyPath("(Position)"));

            doubleAnimation1.To = width;
            doubleAnimation2.To = height;
            pointAnimation.To = position;

            if (completed != null)
                storyboard.Completed += completed;

            storyboard.Begin();
        }
    }
}
